package org.ikastola

import grails.plugins.springsecurity.Secured
import ikastola.Constants;
import beans.PaymentStudentRecord;
import beans.StudentBalanceRecord
import org.springframework.dao.DataIntegrityViolationException

@Secured(['ROLE_ADMIN'])
class BalanceFeesController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	static balanceFeesService

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
    }
	
	def getBalance(params) {
		
		int month = Integer.parseInt(params.month_month)
		int year = Integer.parseInt(params.month_year)
		Long course = Long.parseLong(params.course)
		Long fee = Long.parseLong(params.fee)
		
		def courseInstance = Course.get(course)
		
		if (!courseInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), course])
			redirect(action: "list")
			return
		}
		def feeInstance = Fee.get(fee)
		if (!feeInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'fee.label', default: 'Fee'), fee])
			redirect(action: "list")
			return
		}
		
		List<StudentBalanceRecord> balances = balanceFeesService.getBalancesByCourseAndDate(courseInstance, month, year, feeInstance)
		
//		for (b in balances) {
//			System.out.print(b.getFullName()) + " " + System.out.println(b.getDocnumber()) 
//			System.out.println(b.getTotalPayments())
//			System.out.println("Fee Anuales:")
//			for (a in b.getAnnualFees().keySet())	{
//				def fees = b.getAnnualFees().get(a)
//				System.out.println("Año: "+ a)
//				for (f in fees) {
//					System.out.println("Fee:" + f.getFee().getName() + "- pago: " + f.getAmount())
//				}
//			}
//			System.out.println("Fee Mensuales:")
//			for (m in b.getMonthlyFees().keySet())	{
//				def fees = b.getMonthlyFees().get(m)
//				System.out.println("Mes: "+ m)
//				for (f in fees) {
//					System.out.println("Fee: " + f.getFee().getName() + "- pago: " + f.getAmount())
//				} 
//			}
//		}
		List<PaymentStudentRecord> reportBeans = []
		if (Constants.FEE_PERIOD_MONTH.equals(feeInstance.getPeriod())) {
			reportBeans = balanceFeesService.getMonthlyPaymentStudentBeans(balances)
			params._file = "monthly"
		} else {
			reportBeans = balanceFeesService.getAnnualPaymentStudentBeans(balances)
			params._file = "annual"
			params.year = params.month_year
		}
		
		if (params.pdf) {
			params._format = "PDF"
			params._name = "pagos-alumnos-"+courseInstance.getName()+courseInstance.getDivision()
			params.enddate = params.month_month + " / " + params.month_year
			params.course = courseInstance.getName() + " " + courseInstance.getDivision()
			params.fee = feeInstance.getName()
	
			chain(controller:'jasper',action:'index',model:[data:reportBeans],params:params)
			return
		}
		else {
			render(template: "form", model: [reportBeans: reportBeans, month: params.month_month, year: params.month_year, course: params.course, fee: params.fee ])
		}

	}
}
